{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from scipy.stats import ttest_ind\n",
    "from scipy.stats import shapiro\n",
    "from  utils import column_letter_to_index\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from scipy import stats\n",
    "from scipy.stats import mannwhitneyu\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------正态性检验--------------\n",
      "总体的正态分布检验\n",
      "Shapiro-Wilk正态性检验结果：\n",
      "统计量： 0.9378930926322937\n",
      "p值： 1.3209255911306172e-10\n",
      "-----------独立性分析------------\n",
      "Mann-Whitney  检验\n",
      "Mann-Whitney U统计量： 1430.5\n",
      "p-value： 0.02921563015728457\n"
     ]
    }
   ],
   "source": [
    "#数据读取\n",
    "data_path = '../data/raw_data/334份 按选项序号 汇总变量后.xlsx'\n",
    "\n",
    "df = pd.read_excel(data_path)  \n",
    "\n",
    "# 1111111\n",
    "gender_idx = column_letter_to_index('AK')#X变量，不同差异的\n",
    "ele_idx = column_letter_to_index('AT')#Y变量\n",
    "binary_value = [1,2]#Excel里X变量的所有取值，例如[1,2,3,4] ，逗号用英文\n",
    "\n",
    "#-------------------------------------------------\n",
    "# 提取需要分析的列\n",
    "columns_to_analyze = [gender_idx, ele_idx]  # 将M和N替换为您需要的列索引\n",
    "selected_data = df.iloc[:, columns_to_analyze]\n",
    "\n",
    "# 提取性别和电子健康素养得分列\n",
    "gender = selected_data.iloc[:,0]\n",
    "score = selected_data.iloc[:,1]\n",
    "\n",
    "# binary_value = [1,2]\n",
    "\n",
    "# 拆分性别为两个独立的组\n",
    "group1 = score[gender == binary_value[0]]\n",
    "group2 = score[gender == binary_value[1]]\n",
    "\n",
    "print('--------------正态性检验--------------')\n",
    "\n",
    "\n",
    "print('总体的正态分布检验')\n",
    "stat, p_value = shapiro(score)\n",
    "print(\"Shapiro-Wilk正态性检验结果：\")\n",
    "print(\"统计量：\", stat)\n",
    "print(\"p值：\", p_value)\n",
    "\n",
    "# print('{} 的正态分布检验'.format(binary_value[0]))\n",
    "# stat, p_value = shapiro(group1)\n",
    "# print(\"Shapiro-Wilk正态性检验结果：\")\n",
    "# print(\"统计量：\", stat)\n",
    "# print(\"p值：\", p_value)\n",
    "\n",
    "# print('{} 的正态分布检验'.format(binary_value[1]))\n",
    "# stat, p_value = shapiro(group2)\n",
    "# print(\"Shapiro-Wilk正态性检验结果：\")\n",
    "# print(\"统计量：\", stat)\n",
    "# print(\"p值：\", p_value)\n",
    "\n",
    "\n",
    "print('-----------独立性分析------------')\n",
    "statistic, p_value = mannwhitneyu(group1, group2)\n",
    "\n",
    "# t_statistic, t_p_value = ttest_ind(group1, group2)\n",
    "\n",
    "# print('t 检验')\n",
    "# print(\"t统计量：\", t_statistic)\n",
    "# print(\"p-value：\", t_p_value)\n",
    "\n",
    "print('Mann-Whitney  检验')\n",
    "print(\"Mann-Whitney U统计量：\", statistic)\n",
    "print(\"p-value：\", p_value)\n",
    "# 打印检验结果\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "# plt.hist(group1, bins=10, alpha=0.5, label=gender)\n",
    "\n",
    "# # 设置图表标题、横轴和纵轴标签\n",
    "# plt.title(\"Distribution of Electronic Health Literacy by Gender\")\n",
    "# plt.xlabel(\"Score\")\n",
    "# plt.ylabel(\"Frequency\")\n",
    "\n",
    "# # 添加图例\n",
    "# plt.legend()\n",
    "\n",
    "# # 显示图表\n",
    "# plt.show()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 结论\n",
    "P 太小不符合正态分布，所以用Mann-Whitney  检验\n",
    "\n",
    "根据您提供的结果，Mann-Whitney U统计量为11687.5，p-value为0.0308（约为0.031）。由于p-value小于通常使用的显著性水平0.05，可以得出结论：在电子健康素养方面，男性和女性之间存在显著差异。\n",
    "\n",
    "请注意，这个结果表明，根据样本数据，有足够的证据支持男性和女性在电子健康素养方面的差异，但不能确定差异的方向（即哪个性别的得分更高）。此外，需要考虑到样本的大小、数据的分布以及其他研究设计上的因素来正确解释和应用这个结果。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "whj",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
